package com.young.common.expression.dns;

import java.net.InetAddress;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:47 PM
 */
public class CommonResolver extends PropertyResolver {
    private static final NamingResolver DEFAULT;

    public CommonResolver() {
    }

    public InetAddress[] resolve(String target) {
        if (NamingResolver.isIpAddress(target)) {
            return DEFAULT.resolve(target);
        } else {
            return NamingResolver.isDomain(target) ? this.domainResolve(target) : this.serviceResolve(target);
        }
    }

    public InetAddress[] domainResolve(String domain) {
        String value = ThreadLocalResolverSupporter.findDomain(domain);
        return value != null ? DEFAULT.resolve(value) : super.domainResolve(domain);
    }

    public InetAddress[] serviceResolve(String service) {
        String value = ThreadLocalResolverSupporter.findService(service);
        return value != null ? DEFAULT.resolve(value) : super.serviceResolve(service);
    }

    public String findService(String service) {
        String value = ThreadLocalResolverSupporter.findService(service);
        return value == null ? super.findService(service) : value;
    }

    public Map<String, String> copyDomainResolvers() {
        DnsResolverTransmittableEnum transmittable = ThreadLocalResolverSupporter.getTransmittable();
        switch (transmittable) {
            case DEFAULT:
                return ThreadLocalResolverSupporter.getDomainResolvers();
            case ALL:
                Map<String, String> domainResolvers = ThreadLocalResolverSupporter.getDomainResolvers();
                super.copyDomainResolvers().forEach((key, value) -> {
                    if (!domainResolvers.containsKey(key)) {
                        domainResolvers.put(key, value);
                    }

                });
                return domainResolvers;
            default:
                return null;
        }
    }

    public Map<String, String> copyServiceResolvers() {
        DnsResolverTransmittableEnum transmittable = ThreadLocalResolverSupporter.getTransmittable();
        switch (transmittable) {
            case DEFAULT:
                return ThreadLocalResolverSupporter.getServiceResolvers();
            case ALL:
                Map<String, String> serviceResolvers = ThreadLocalResolverSupporter.getServiceResolvers();
                super.copyServiceResolvers().forEach((key, value) -> {
                    if (!serviceResolvers.containsKey(key)) {
                        serviceResolvers.put(key, value);
                    }

                });
                return serviceResolvers;
            default:
                return null;
        }
    }

    public Map<String, String> copyServiceGroupResolvers() {
        DnsResolverTransmittableEnum transmittable = ThreadLocalResolverSupporter.getTransmittable();
        switch (transmittable) {
            case DEFAULT:
            case ALL:
                return ThreadLocalResolverSupporter.getServiceGroupResolvers();
            default:
                return null;
        }
    }

    static {
        DEFAULT = DefaultResolver.INSTANCE;
    }

}
